package com.yonyou.iuap.jxzy.ref.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.ref.model.RefViewModelVO;

/**
 * 货位参照查询
 * @author 21559
 *
 */
@Controller
@RequestMapping(value="/rackref")
public class rackRefController extends AbstractBillRefModel{
	

	@Override
	public String[] getStrFieldCode() {
		return new String[]{"code","name"};
	}

	@Override
	public String getTableName() {
		return " bd_rack_app_view ";
	}

	@Override
	public String[] getStrFieldName() {
		return new String[]{"编码","名称"};
	}

	@Override
	public String getRefCode() {
		return "code";
	}

	@Override
	public String getRefName() {
		return "name";
	}

	@Override
	public String getRefID() {
		return " pk_rack ";
	}

	@Override
	public String getWhereSQL(RefViewModelVO refModel) {
		String sqlcond="";
		String pk_org = refModel.getPk_org();
		if(pk_org!=null){
			sqlcond+=" and pk_org in (select pk_org from org_orgs where name='"+pk_org+"')";
		}
		String content = refModel.getContent();
		if(content!=null){
			JSONObject parseObject = JSONObject.parseObject(content);
			Object type = parseObject.get("type");//1为进口，其余保持原逻辑
			Object rootName = parseObject.get("rootName");//物料主键
			Object checkDate = parseObject.get("checkDate");//检验时间
			if("1".equals(type)){//进口
				sqlcond+=" and pk_rack in (select DEF8 from levm_heapnumberdoc_view where dr=0 and PK_MATERIAL='"+rootName+"' and '"+checkDate+"' between DEF1 and DEF6 )";
			}else{
				Object checkPoint = parseObject.get("checkPoint");//检验点
				sqlcond+=" and pk_rack in (select DEF8 from levm_heapnumberdoc_view where dr=0 and PK_MATERIAL='"+rootName+"' and def4 in (select pk_defdoc from bd_defdoc where pk_defdoclist='1001G410000000000U3J' and name='"+checkPoint+"') and '"+checkDate+"' between DEF1 and DEF6 )";
			}
			refModel.setContent("1=1");
		}
		sqlcond+=" and dr = 0 ";
		return sqlcond;
	}
	
	

	@Override
	public String initWhereSQL(RefViewModelVO refModel) {
		// TODO Auto-generated method stub
//		return super.initWhereSQL(refModel);
		String wheresql = getWhereSQL(refModel);
		return wheresql;
	}

	@Override
	public List<Map<String, Object>> getRefData(RefViewModelVO refModel) {
		return super.getRefData(refModel);
	}

	@Override
	public Map<String, Object> blobRefSearch(RefViewModelVO refModel) {
		if("app".equals(refModel.getSysId())){
			List<Map<String,Object>> results = getRefData(refModel);
			Map<String, Object> mapList = new HashMap();
			mapList.put("detailMsg", results);
			mapList.put("message", "操作成功");
			mapList.put("success", "success");
			return mapList;
		}
		return super.blobRefSearch(refModel);
	}
	
	
}
